home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / gfx / nsRect.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  8KB  |  209 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38.  
  39. #ifndef NSRECT_H
  40. #define NSRECT_H
  41.  
  42. #include <stdio.h>
  43. #include "nsCoord.h"
  44. #include "nsPoint.h"
  45. #include "nsSize.h"
  46. #include "nsMargin.h"
  47. #include "nsUnitConversion.h"
  48. #include "gfxCore.h"
  49.  
  50. struct NS_GFX nsRect {
  51.   nscoord x, y;
  52.   nscoord width, height;
  53.  
  54.   // Constructors
  55.   nsRect() : x(0), y(0), width(0), height(0) {}
  56.   nsRect(const nsRect& aRect) {*this = aRect;}
  57.   nsRect(const nsPoint& aOrigin, const nsSize &aSize) {x = aOrigin.x; y = aOrigin.y;
  58.                                                        width = aSize.width; height = aSize.height;}
  59.   nsRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
  60.     x = aX; y = aY; width = aWidth; height = aHeight;
  61.     VERIFY_COORD(x); VERIFY_COORD(y); VERIFY_COORD(width); VERIFY_COORD(height);
  62.   }
  63.  
  64.   // Emptiness. An empty rect is one that has no area, i.e. its height or width
  65.   // is <= 0
  66.   PRBool IsEmpty() const {
  67.     return (PRBool) ((height <= 0) || (width <= 0));
  68.   }
  69.   void   Empty() {width = height = 0;}
  70.  
  71.   // Containment
  72.   PRBool Contains(const nsRect& aRect) const;
  73.   PRBool Contains(nscoord aX, nscoord aY) const;
  74.   PRBool Contains(const nsPoint& aPoint) const {return Contains(aPoint.x, aPoint.y);}
  75.  
  76.   // Intersection. Returns TRUE if the receiver overlaps aRect and
  77.   // FALSE otherwise
  78.   PRBool Intersects(const nsRect& aRect) const;
  79.  
  80.   // Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
  81.   // the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
  82.   // rect to be an empty rect.
  83.   //
  84.   // 'this' can be the same object as either aRect1 or aRect2
  85.   PRBool IntersectRect(const nsRect& aRect1, const nsRect& aRect2);
  86.  
  87.   // Computes the smallest rectangle that contains both aRect1 and aRect2 and
  88.   // fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
  89.   // empty rect if both aRect1 and aRect2 are empty
  90.   //
  91.   // 'this' can be the same object as either aRect1 or aRect2
  92.   PRBool UnionRect(const nsRect& aRect1, const nsRect& aRect2);
  93.  
  94.   // Accessors
  95.   void SetRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
  96.     x = aX; y = aY; width = aWidth; height = aHeight;
  97.   }
  98.   void MoveTo(nscoord aX, nscoord aY) {x = aX; y = aY;}
  99.   void MoveTo(const nsPoint& aPoint) {x = aPoint.x; y = aPoint.y;}
  100.   void MoveBy(nscoord aDx, nscoord aDy) {x += aDx; y += aDy;}
  101.   void MoveBy(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y;}
  102.   void SizeTo(nscoord aWidth, nscoord aHeight) {width = aWidth; height = aHeight;}
  103.   void SizeTo(const nsSize& aSize) {SizeTo(aSize.width, aSize.height);}
  104.   void SizeBy(nscoord aDeltaWidth, nscoord aDeltaHeight) {width += aDeltaWidth;
  105.                                                           height += aDeltaHeight;}
  106.  
  107.   // Inflate the rect by the specified width/height or margin
  108.   void Inflate(nscoord aDx, nscoord aDy);
  109.   void Inflate(const nsSize& aSize) {Inflate(aSize.width, aSize.height);}
  110.   void Inflate(const nsMargin& aMargin);
  111.  
  112.   // Deflate the rect by the specified width/height or margin
  113.   void Deflate(nscoord aDx, nscoord aDy);
  114.   void Deflate(const nsSize& aSize) {Deflate(aSize.width, aSize.height);}
  115.   void Deflate(const nsMargin& aMargin);
  116.  
  117.   // Overloaded operators. Note that '=' isn't defined so we'll get the
  118.   // compiler generated default assignment operator.
  119.   PRBool  operator==(const nsRect& aRect) const {
  120.     return (PRBool) ((IsEmpty() && aRect.IsEmpty()) ||
  121.                      ((x == aRect.x) && (y == aRect.y) &&
  122.                       (width == aRect.width) && (height == aRect.height)));
  123.   }
  124.   PRBool  operator!=(const nsRect& aRect) const {
  125.     return (PRBool) !operator==(aRect);
  126.   }
  127.  
  128.   nsRect  operator+(const nsPoint& aPoint) const {
  129.     return nsRect(x + aPoint.x, y + aPoint.y, width, height);
  130.   }
  131.   nsRect  operator-(const nsPoint& aPoint) const {
  132.     return nsRect(x - aPoint.x, y - aPoint.y, width, height);
  133.   }
  134.   nsRect& operator+=(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y; return *this;}
  135.   nsRect& operator-=(const nsPoint& aPoint) {x -= aPoint.x; y -= aPoint.y; return *this;}
  136.  
  137.   nsRect& operator*=(const float aScale) {x = NSToCoordRound(x * aScale); 
  138.                                           y = NSToCoordRound(y * aScale); 
  139.                                           width = NSToCoordRound(width * aScale); 
  140.                                           height = NSToCoordRound(height * aScale); 
  141.                                           return *this;}
  142.  
  143.   nsRect& ScaleRoundOut(const float aScale);
  144.   nsRect& ScaleRoundIn(const float aScale);
  145.  
  146.   // Helpers for accessing the vertices
  147.   nsPoint TopLeft() const { return nsPoint(x, y); }
  148.   nsPoint TopRight() const { return nsPoint(XMost(), y); }
  149.   nsPoint BottomLeft() const { return nsPoint(x, YMost()); }
  150.   nsPoint BottomRight() const { return nsPoint(XMost(), YMost()); }
  151.  
  152.   nsSize Size() const { return nsSize(width, height); }
  153.  
  154.   // Helper methods for computing the extents
  155.   nscoord XMost() const {return x + width;}
  156.   nscoord YMost() const {return y + height;}
  157. };
  158.  
  159. #ifdef NS_COORD_IS_FLOAT
  160. struct NS_GFX nsIntRect {
  161.   PRInt32 x, y;
  162.   PRInt32 width, height;
  163.  
  164.   // Constructors
  165.   nsIntRect() : x(0), y(0), width(0), height(0) {}
  166.   nsIntRect(const nsIntRect& aRect) {*this = aRect;}
  167.   nsIntRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
  168.     x = aX; y = aY; width = aWidth; height = aHeight;
  169.   }
  170.  
  171.   // Emptiness. An empty rect is one that has no area, i.e. its height or width
  172.   // is <= 0
  173.   PRBool IsEmpty() const {
  174.     return (PRBool) ((height <= 0) || (width <= 0));
  175.   }
  176.  
  177.   void SetRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
  178.     x = aX; y = aY; width = aWidth; height = aHeight;
  179.   }
  180.  
  181.   // Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
  182.   // the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
  183.   // rect to be an empty rect.
  184.   //
  185.   // 'this' can be the same object as either aRect1 or aRect2
  186.   PRBool IntersectRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
  187.  
  188.   // Computes the smallest rectangle that contains both aRect1 and aRect2 and
  189.   // fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
  190.   // empty rect if both aRect1 and aRect2 are empty
  191.   //
  192.   // 'this' can be the same object as either aRect1 or aRect2
  193.   PRBool UnionRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
  194.  
  195.   // Helper methods for computing the extents
  196.   PRInt32 XMost() const {return x + width;}
  197.   PRInt32 YMost() const {return y + height;}
  198. };
  199. #else
  200. typedef nsRect nsIntRect;
  201. #endif
  202.  
  203. #ifdef DEBUG
  204. // Diagnostics
  205. extern NS_GFX FILE* operator<<(FILE* out, const nsRect& rect);
  206. #endif // DEBUG
  207.  
  208. #endif /* NSRECT_H */
  209.